package com.zy.experience_system.controller;


import com.zy.experience_system.enums.PermsEnum;
import com.zy.experience_system.enums.ResultEnum;
import com.zy.experience_system.exception.CommonException;
import com.zy.experience_system.form.TestSetForm;
import com.zy.experience_system.service.TestSetService;
import com.zy.experience_system.util.ResultUtil;
import com.zy.experience_system.vo.ResultVO;
import com.zy.experience_system.vo.StuSetVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/set")
public class SetController extends BaseController {

    @Autowired
    private TestSetService testSetService;

    @PostMapping("/create")
    public ResultVO create(
            @Validated @RequestBody TestSetForm testSetForm) throws Exception {
        if (claims.get("perms").equals(PermsEnum.TEACHER.getPerms())) {
            try {
                Integer setId = testSetService.save(testSetForm);
                Map<String, Integer> map = new HashMap<>();
                map.put("setId", setId);
                return ResultUtil.success(map);
            } catch (Exception e) {
                return ResultUtil.error("创建失败");
            }
        }
        throw new CommonException(ResultEnum.NO_PERMS);
    }

    @PutMapping("/update")
    public ResultVO update(
            @Validated @RequestBody TestSetForm testSetForm) throws Exception {
        if (claims.get("perms").equals(PermsEnum.TEACHER.getPerms())) {
            if (claims.getId().equals(testSetForm.getTeachId())) {
                if (testSetForm.getSetId() != null) {
                    try {
                        testSetService.save(testSetForm);
                        return ResultUtil.success(null);
                    } catch (Exception e) {
                        return ResultUtil.error("修改失败");
                    }
                }
                throw new CommonException(ResultEnum.FAIL);
            }
            throw new CommonException(ResultEnum.NO_PERMS);
        }
        throw new CommonException(ResultEnum.NO_PERMS);
    }

    @DeleteMapping("/delete")
    public ResultVO delete(
            @Validated @RequestBody TestSetForm testSetForm) throws Exception {
        if (claims.get("perms").equals(PermsEnum.TEACHER.getPerms())) {
            if (claims.getId().equals(testSetForm.getTeachId())) {
                try {
                    testSetService.delete(testSetForm);
                    return ResultUtil.success(null);
                } catch (Exception e) {
                    return ResultUtil.error("删除失败");
                }
            }
            throw new CommonException(ResultEnum.NO_PERMS);
        }
        throw new CommonException(ResultEnum.NO_PERMS);
    }

    @GetMapping("/find/{setId}")
    public ResultVO find(@PathVariable("setId") Integer setId) throws Exception {
        try {
            StuSetVO stuSetVO = testSetService.find(setId);
            return ResultUtil.success(stuSetVO);
        } catch (Exception e) {
            return ResultUtil.error("查询失败");
        }
    }


    @GetMapping("/findAll")
    public ResultVO findAll() throws Exception {
        if (claims.get("perms").equals(PermsEnum.ADMIN.getPerms())) {
            try {
                List<StuSetVO> stuSetVOList = testSetService.findAll();
                return ResultUtil.success(stuSetVOList);
            } catch (Exception e) {
                return ResultUtil.error("查询失败");
            }
        }
        throw new CommonException(ResultEnum.NO_PERMS);
    }

    @GetMapping("/findByTeach/{teachId}")
    public ResultVO findByTeach(@PathVariable("teachId") String teachId) throws Exception {
        if (claims.get("perms").equals(PermsEnum.TEACHER.getPerms())) {
            if (claims.getId().equals(teachId)) {
                try {
                    List<StuSetVO> stuSetVOList = testSetService.findByTeachId(teachId);
                    return ResultUtil.success(stuSetVOList);
                } catch (Exception e) {
                    return ResultUtil.error("查询失败");
                }
            }
            throw new CommonException(ResultEnum.NO_PERMS);
        }
        throw new CommonException(ResultEnum.NO_PERMS);
    }
}
